use lib "../";
use BuildTool;


use Test::More tests => 5;
use Test::Exception;
Test::More->builder->output ('Test_Report/Test_Cases/ts1_result.txt');
Test::More->builder->failure_output ('Test_Report/Test_Cases/ts1_errors.txt');
 
 BEGIN {

 	use_ok(Build);
 }
 
subtest 'Test Case 1.1' => sub {
	plan tests => 1;
####################################################################################
#	
# Verifying baseline number -not the first baseline and build
#
####################################################################################
	#Not the first baseline
	$prev_type = "base2.1";
	my $first_build = "false";
	my $type = "baseline";
	my $number = BuildTool->defineBuildNumber($first_build,$prev_type,$type); 	
	is($number, "3.1", "Verifying that the baseline number is 3.1");
	
};
 
subtest 'Test Case 1.2' => sub {
	plan tests => 1;
####################################################################################
#	
# Verifying baseline number - first baseline and build
#
####################################################################################
	#First build and baseline - The number will be 1.1
	my $prev_type = "";
	my $first_build = "false";
	my $type = "baseline";
	my $number = BuildTool->defineBuildNumber($first_build,$prev_type,$type); 	
	is($number, "1.1", "Verifying that the first build number is 1.1");
};
 
subtest 'Test Case 1.3' => sub {
	plan tests => 4;
####################################################################################
#	
# Verifying cron number
#
####################################################################################
	#Previous build is a cron
	my $prev_type = "cron1.2";
	my $first_build = "false";
	my $type = "cron";
	my $number = BuildTool->defineBuildNumber($first_build,$prev_type,$type); 	
	is($number, "1.3", "Verifying that the cron number is 1.3");
	
	#Previous build is a baseline
	my $prev_type = "base1.1";
	my $first_build = "false";
	my $type = "cron";
	my $number = BuildTool->defineBuildNumber($first_build,$prev_type,$type); 	
	is($number, "1.2", "Verifying that the cron number is 1.2");
	
	#Previous build has more than one digit in the major
	my $prev_type = "base11.1";
	my $first_build = "false";
	my $type = "cron";
	my $number = BuildTool->defineBuildNumber($first_build,$prev_type,$type); 	
	is($number, "11.2", "Verifying that the cron number is 11.2");
	
	#Previous build has more than one digit in the minor
	my $prev_type = "cron11.12";
	my $first_build = "false";
	my $type = "cron";
	my $number = BuildTool->defineBuildNumber($first_build,$prev_type,$type); 	
	is($number, "11.13", "Verifying that the cron number is 11.13");
};
 
subtest 'Test Case 1.4' => sub {
	plan tests => 9;
####################################################################################
#	
# Verifying that the tools gets the correct params from buildParams.txt
#
####################################################################################
 	$buildParamsPath = "http://cad.medtronic.com/repo/crdm/device/firmware/trunk/tools/build_tool/buildParams/playground.txt";
 	my $buildParams = VCS->checkOutBuildParams($buildParamsPath);
 	is($buildParams,"playground.txt",'verifying buildParams');
 	#Read the buildParams.txt
 	my $params_ref = BuildTool->readParams($buildParams);
 	@params = @$params_ref;
 	#Compare the values are equal to expected
 	is($params[0],'http://cad-test','Verifying buildParams.txt polarion_path');
 	is($params[1],'http://cad-test/repo/crdm/device/firmware/tools/build_tool','Verifying buildParams.txt svn_path');
 	is($params[2],'trunk/quals/main_fw','Verifying buildParams.txt code_directory');
 	
 	my $conf_ref = $params[3];
 	my %conf_hash = %$conf_ref; 
 	is(@{$conf_hash{"Debug"}}[0],'0','Verifying configuration name/number mapping for Debug');
    is(@{$conf_hash{"Release"}}[0],'1','Verifying configuration name/number mapping for Release');
 	is(@{$conf_hash{"ReleaseRTLx"}}[0],'2','Verifying configuration name/number mapping for ReleaseRTLx');
 	#buildParams.txt doesn't exist in this case
 	$buildParams = "ThisFileDoesntExist.txt";
 	dies_ok{BuildTool->readParams($buildParams)} 'Verifying if dies with inexistent file';
 	#buildParams.txt is empty in this case
 	$buildParams = "emptyBuildParams.txt";
 	dies_ok{BuildTool->readParams($buildParams)} 'Verifying if dies with empty file';

 	system("rm -rf playground.txt");	
 };

 